//
//  AdViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/1.
//

import UIKit

class AdViewController: CommonViewController {
    var completion:(()->Void)?
    private var done = false
    private var count = 3
    private let stateButton = UIButton.init(type: .custom)
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let tip = UILabel()
        self.view.addSubview(tip)
        tip.textColor = .white
        tip.font = Font_Size_12
        tip.text = "广告 已预加载"
        tip.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6)
        tip.setRadiusAndDirection(radius: 5)
        tip.snp.makeConstraints({
            if #available(iOS 11.0, *) {
                $0.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(15)
            } else {
                // Fallback on earlier versions
                $0.top.equalTo(15)
            }
            $0.right.equalToSuperview().inset(30)
        })
        self.view.backgroundColor = .green
        // 倒计时
        WJGCDTimer.default.scheduledDispatchTimer(withName: "timer", timerInterval: 1) {
            guard self.count>1 else{
                self.dismiss(animated: true) {
                    WJGCDTimer.default.destoryTimer(withName: "timer")
                }
                return
            }
            self.count -= 1
            self.stateButton.setTitle("跳过广告 \(self.count)", for: .normal)
        }
        stateButton.setRadiusAndDirection(radius: 5)
        stateButton.setBackgroundImage(UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6).toImage(), for: .normal)
        stateButton.setTitle("跳过广告 \(self.count)", for: .normal)
        stateButton.titleLabel?.font = Font_Size_12
        stateButton.contentEdgeInsets = UIEdgeInsets.init(top: 5, left: 10, bottom: 5, right: 10)
        view.addSubview(stateButton)
        stateButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(30)
            if #available(iOS 11.0, *) {
                make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).inset(15)
            } else {
                // Fallback on earlier versions
                make.bottom.equalToSuperview().inset(15)
            }
        }
        stateButton.addTarget(self, action: #selector(click), for: .touchUpInside)
        self.navigationController?.isNavigationBarHidden = true
        self.statusBarHidden = true
    }
 
    func nextStep() {
        guard !done else {
            return
        }
        if(self.count > 0 ){
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1) {
                SystemLog("\(self.count)")
                self.count -= 1
                self.stateButton.setTitle("跳过广告 \(self.count)", for: .normal)
                self.nextStep()
            }
        }else{
            if(!self.done){
                self.dismiss(animated: true) {
                    
                }
            }
        }
    }
    @objc
    func click() {
        self.done = true
        self.dismiss(animated: true) {
            
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
